package zmee.src.formplugin;

import kd.bos.bill.AbstractBillPlugIn;
import kd.bos.bill.BillShowParameter;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.entity.datamodel.IDataModel;
import kd.bos.form.FormShowParameter;
import kd.bos.form.IFormView;
import kd.bos.form.control.Control;
import kd.bos.form.events.BeforeDoOperationEventArgs;
import kd.bos.form.operate.FormOperate;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.scm.common.util.BOTPUtil;
import org.apache.commons.lang.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author yss
 * @createTime 2022-11-28 6:56
 * @description 资质后审表单插件
 */
public class AptitudeAuditBillPlugin extends AbstractBillPlugIn {

    @Override
    public void beforeDoOperation(BeforeDoOperationEventArgs args) {
        super.beforeDoOperation(args);

        FormOperate formOperate=(FormOperate)args.getSource();
        String key = formOperate.getOperateKey();
        if("submit".equals(key)){
            //一、主单据数据
            //1.1 启动项目编码
            String billno = (String) this.getModel().getValue("billno");
            //根据编码获取上游单据启动项目的数据
            DynamicObject srcProjectDy = BusinessDataServiceHelper.loadSingleFromCache("src_project", "zmee_dczb",
                    new QFilter[]{new QFilter("billno", QCP.equals, billno)});
            if(srcProjectDy == null){
                beforeDoOperation_over(args,"单据编码错误");
                return;
            }
            boolean dczb = srcProjectDy.getBoolean("zmee_dczb");//是否多次招标

            //二、子单据数据
            //1、获取审批明细的pageId
            String resultPageId = this.getView().getPageCache().get("tabpageap2");
            if(StringUtils.isEmpty(resultPageId)){
                beforeDoOperation_over(args,"未找到子页面id,数据错误");
                return;
            }
            //2、获取审批明细上的试图
            IFormView resultView = this.getView().getView(resultPageId);
            //3、获取审批明细上的模型
            IDataModel model = resultView.getModel();
            //4、获取审批明细上的数据
            DynamicObject resultDy = model.getDataEntity(true);
            //若“资审状态”字段为“已评分”且“资审结果”字段为“资审合格”的供应商数量<3
            DynamicObjectCollection resultEntryDyCo = resultDy.getDynamicObjectCollection("entryentity");
            List<DynamicObject> resultEntryList = resultEntryDyCo.stream().filter(kf -> {
                DynamicObject scoreTaskDy = kf.getDynamicObject("scoretask");//资审单号
                String bizstatus = scoreTaskDy.getString("bizstatus");
                String isaptitude = kf.getString("isaptitude");
                return "D".equals(bizstatus) && "1".equals(isaptitude);
            }).collect(Collectors.toList());
            if(resultEntryList.size() < 3){
                String typeStr = dczb ? "0" : "1";
                //二次确认
                FormShowParameter formShowParameter = new BillShowParameter();
                Map<String, Object> customMap = new HashMap<>();
                customMap.put("type",typeStr);
                customMap.put("isfirst",true);
                formShowParameter.setCustomParams(customMap);
                this.getView().showForm(formShowParameter);
            }
        }
    }

    private void beforeDoOperation_over(BeforeDoOperationEventArgs args,String errorStr){
        args.setCancel(true);
        this.getView().showErrorNotification(errorStr);
    }
}
